USE [edb_sigar_prd]
GO
/****** Object:  StoredProcedure [dbo].[SP_SIGAR_REP_PEDIDO_POSICION_CXA]    Script Date: 1/13/2015 9:09:20 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SP_SIGAR_REP_PEDIDO_POSICION_CXA]
		@checkpoint_id bigint
AS
BEGIN
    SET NOCOUNT ON;
    
  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

  BEGIN TRY;
    BEGIN TRANSACTION;

	INSERT INTO [dbo].[SGTM_OPERACION_CUENTA_FACT_PROV] 
	SELECT
        CP.[CHECKPOINT_ID] ,
        CP.[SOCIEDAD_ID] ,
        CP.NUMERO_DOCUMENTO_COMPRA ,
		CP.USUARIO,
        CP.[ACREEDOR_ID] ,
        CP.[ACREEDOR_RAZON_SOCIAL] ,
        count(distinct CUENTA_CONTABLE_ID) cantidadCuentaContable,
        count(distinct posicion) cantidadPosicion,
        dbo.FN_FACTURA_DIRECTA(CP.[CHECKPOINT_ID],CP.[SOCIEDAD_ID],CP.NUMERO_DOCUMENTO_COMPRA) cantidadFactura,
        dbo.FN_PROVISION_FACTURA('S',CP.[CHECKPOINT_ID],CP.[SOCIEDAD_ID],CP.NUMERO_DOCUMENTO_COMPRA) cantidadProvicionFactura,
        dbo.FN_PROVISION_FACTURA('N',CP.[CHECKPOINT_ID],CP.[SOCIEDAD_ID],CP.NUMERO_DOCUMENTO_COMPRA) cantidadProvisionSinFactura,
        SUM(MONTO_IMPORTE_VALORADO) ,
		SUM(MONTO_MONEDA_LOCAL) 
        FROM
        [dbo].[SGTM_OPERACION_CUENTA] CP
            INNER JOIN dbo.SGTM_SOCIEDAD S ON CP.SOCIEDAD_ID = S.SOCIEDAD_ID
            WHERE 
				CP.CHECKPOINT_ID = @checkpoint_id
				AND CP.NUMERO_DOCUMENTO_COMPRA IS NOT NULL
           group by CP.[CHECKPOINT_ID] ,
        CP.[SOCIEDAD_ID] ,
        CP.NUMERO_DOCUMENTO_COMPRA ,
		CP.USUARIO,
        CP.[ACREEDOR_ID] ,
        CP.[ACREEDOR_RAZON_SOCIAL]   
    
    

    COMMIT TRANSACTION;
  END TRY
  BEGIN CATCH
    IF (@@TRANCOUNT > 0)
      ROLLBACK TRANSACTION;

    DECLARE 
      @ErrorMessage nvarchar(4000),
      @ErrorNumber int,
      @ErrorSeverity int,
      @ErrorState int,
      @ErrorLine int,
      @ErrorProcedure nvarchar(200);

    -- Assign variables to error-handling functions that 
    -- capture information for RAISERROR.
    SELECT 
        @ErrorNumber = ERROR_NUMBER(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE(),
        @ErrorLine = ERROR_LINE(),
        @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');

    -- Building the message string that will contain original
    -- error information.
    SELECT @ErrorMessage = 
        N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' + 
            'Message: '+ ERROR_MESSAGE();

    -- Raise an error: msg_str parameter of RAISERROR will contain
    -- the original error information.
    RAISERROR (
      @ErrorMessage, 
      @ErrorSeverity, 
      1,               
      @ErrorNumber,    -- parameter: original error number.
      @ErrorSeverity,  -- parameter: original error severity.
      @ErrorState,     -- parameter: original error state.
      @ErrorProcedure, -- parameter: original error procedure name.
      @ErrorLine       -- parameter: original error line number.
    );
  END CATCH
END
